package com.be.beadmin.product.controller;

import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.product.dto.output.WorkingHoursCountEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;

import com.be.beadmin.product.service.WorkingHoursCountService;
import com.be.beadmin.product.dto.input.WorkingHoursCountQueryCriteria;
import com.be.beadmin.product.dto.input.WorkingHoursCountDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;

/**
* @author zjm
* @date 2023-12-08
**/
@Api(tags = "生产-工时统计")
@RestController
public class WorkingHoursCountController {

    @Autowired
    private  WorkingHoursCountService workingHoursCountService;

    @ApiOperation(value = "分页查询工时统计的数据")
    @PostMapping(value = "/product/getWorkingHoursCountByPage")
    public ResultModel getWorkingHoursCountByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(workingHoursCountService.getWorkingHoursCountByPage(pageEntity)) ;
    }

    @PostMapping("/product/addWorkingHoursCount")
    public ResultModel addWorkingHoursCount(@RequestBody WorkingHoursCountDto workingHoursCountDto){
        workingHoursCountService.addWorkingHoursCount(workingHoursCountDto);
        return ResultModel.success(ResultModel.ADD);
    }
    @ApiOperation(value = "删除工时统计的数据")
    @PostMapping("/product/delWorkingHoursCount")
    public ResultModel delWorkingHoursCount(@RequestBody WorkingHoursCountDto workingHoursCountDto){
        workingHoursCountService.delWorkingHoursCount(workingHoursCountDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }
    @ApiOperation(value = "删除所有工时统计的数据")
    @PostMapping("/product/delAllWorkingHoursCount")
    public ResultModel delWorkingHoursCount(){
        workingHoursCountService.delWorkingHoursCount();
        return ResultModel.success(ResultModel.DELETE);
    }

    @PostMapping("/product/updateWorkingHoursCount")
    public ResultModel updateWorkingHoursCount(@RequestBody WorkingHoursCountDto workingHoursCountDto){
        workingHoursCountService.updateWorkingHoursCount(workingHoursCountDto);
        return ResultModel.success(ResultModel.UPDATE);
    }
    @ApiOperation(value = "生成工时统计的数据")
    @PostMapping("/product/generateWorkingHoursCount")
    public ResultModel generateWorkingHoursCount(){
        workingHoursCountService.generateWorkingHoursCount();
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportWorkingHoursCount")
    public void exportWorkingHoursCount(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<WorkingHoursCountEx> workingHoursCountPage = workingHoursCountService.getWorkingHoursCountByPage(pageEntity);
        List<WorkingHoursCountEx> workingHoursCountExes = workingHoursCountPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("WorkingHoursCount接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), WorkingHoursCountEx.class).sheet("sheet1").doWrite(workingHoursCountExes);
    }
}